/*******************************************************************************
 * Copyright (c) 2008 Sylvain Sicard. All rights reserved. This program and the accompanying materials are made available under the terms of the GNU Lesser
 * Public License v2.1 which accompanies this distribution, and is available at http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors: Sylvain Sicard - initial API and implementation
 ******************************************************************************/
package fr.jade.resources.apache;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.net.InetAddress;
import java.net.UnknownHostException;

import org.objectweb.fractal.api.NoSuchInterfaceException;
import org.objectweb.fractal.api.control.IllegalBindingException;
import org.objectweb.fractal.api.control.IllegalLifeCycleException;
import org.objectweb.fractal.api.control.LifeCycleController;

import fr.jade.base.JadeBaseComponent;
import fr.jade.fraclite.api.control.NoSuchAttributeException;
import fr.jade.resources.api.HttpConnector;
import fr.jade.resources.api.JkConnector;
import fr.jade.util.JadeException;
import fr.jade.util.ShellCommand;

public class ApacheWrapper extends JadeBaseComponent implements HttpConnector, LifeCycleController {

  // Attributes
  protected static final String   apacheBaseAttName    = "apacheBase";
  protected String                apacheBase;
  
  protected static final String   portAttName          = "port";
  protected int                   port;

  protected static final String[] listAtt              = { portAttName, apacheBaseAttName };

  // Bindings
  protected static final String   jkConnectorItfName   = "jkConnector";
  protected JkConnector           jk;

  protected static final String[] listItf              = { jkConnectorItfName };

  boolean started = false;
  
  public ApacheWrapper() {
  }

  @Override
  public String getFcState() {
    return started ? STARTED : STOPPED;
  }

  @Override
  public void startFc() throws IllegalLifeCycleException {
    if (!started) {
      started = true;
      writeWorkerConfigFile();
      try {
        ShellCommand.syncExec(apacheBase+"/bin/apachectl start", new String[0]);
      } catch (JadeException e) {
        e.printStackTrace();
      }
    }
  }

  @Override
  public void stopFc() throws IllegalLifeCycleException {
    if (started) {
      started = false;
      try {
        ShellCommand.syncExec(apacheBase+"/bin/apachectl stop", new String[0]);
      } catch (JadeException e) {
        e.printStackTrace();
      }
    }
  }

  @Override
  public void bindFc(String itfName, Object itfValue) throws NoSuchInterfaceException, IllegalBindingException, IllegalLifeCycleException {
    if (itfName.equals(jkConnectorItfName)) {
      jk = (JkConnector) itfValue;
    } else {
      super.bindFc(itfName, itfValue);
    }
  }

  @Override
  public void unbindFc(String itfName) throws NoSuchInterfaceException, IllegalBindingException, IllegalLifeCycleException {
    if (itfName.equals(jkConnectorItfName)) {
      jk = null;
    } else {
      super.unbindFc(itfName);
    }
  }

  @Override
  public Object lookupFc(String itfName) throws NoSuchInterfaceException {
    if (itfName.equals(jkConnectorItfName)) {
      return jk;
    } else {
      return super.lookupFc(itfName);
    }
  }

  @Override
  public String[] listFc() {
    return super.concat(listItf, super.listFc());
  }

  @Override
  public String getAttribute(String name) throws NoSuchAttributeException {
    if (name.equals(portAttName)) {
      return Integer.toString(port);
    } else if (name.equals(apacheBaseAttName)) {
      return apacheBase;
    } else {
      return super.getAttribute(name);
    }
  }

  @Override
  public String[] listFcAtt() {
    return super.concat(listAtt, super.listFcAtt());
  }

  @Override
  public void setAttribute(String name, String value) throws NoSuchAttributeException {
    if (name.equals(portAttName)) {
      port = Integer.parseInt(value);
    } else if (name.equals(apacheBaseAttName)) {
      apacheBase = value;
    } else {
      super.setAttribute(name, value);
    }
  }

  @Override
  public int getPort() {
    return port;
  }
  
  @Override
  public String getHost() {
    try {
      return InetAddress.getLocalHost().getHostName();
    } catch (UnknownHostException e) {
      return null;
    }
  }
  
  private void writeWorkerConfigFile() {
    File f = new File(apacheBase+"/conf/workers.properties");
    try {
      PrintStream ps = new PrintStream(f);
      
      ps.println("worker.list=worker1,status");
      ps.println("worker.worker1.port="+jk.getPort());
      ps.println("worker.worker1.host="+jk.getHostName());
      ps.println("worker.worker1.type=ajp13");
      ps.println("worker.worker1.lbfactor=1");

      ps.println("worker.status.type=status");
      
      ps.println("worker.loadbalancer.type=lb");
      ps.println("worker.loadbalancer.balance_workers=worker1");

      ps.close();
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    }
  }

}
